$(function () {
    var loanId = getQueryString("loanId");
    if (loanId == null){
        alert("参数非法");
        window.history.go(-1);
        return;
    }
    vm.info(loanId);
});

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURI(r[2]); return null;
}

function CheckUrl(str) {
    var RegUrl = new RegExp();
    RegUrl.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");//jihua.cnblogs.com
    if (!RegUrl.test(str)) {
        return false;
    }
    return true;
}

Vue.filter('br',function (value) {
    return value.replace(/##/g,"<br/>");
});

var vm = new Vue({
    el:"#app",
    data:{
        loan:{},
        isPhone:false,
    },
    methods:{
        filterStrBr:function (value) {
            return value.replace(/##/g,"<br/>");
        },
        checkUrl:function (str) {
            var RegUrl = new RegExp();
            RegUrl.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");//jihua.cnblogs.com
            if (!RegUrl.test(str)) {
                return false;
            }
            return true;
        },
        info:function (id) {
            $.ajax({
                url:"/thmlfApi/loan/info/"+id,
                dataType:"JSON",
                success:function (result) {
                    if (result.code == 200){
                        vm.loan = result.result;
                        vm.isPhone = !vm.checkUrl(result.result.loanUrl);
                    }else
                        alert(result.message);
                },
                error:function (e) {
                    console.log("error",e);
                    alert("请求失败");
                }
            })
        },
        open:function (url,id) {
            var username = localStorage.getItem('username');
            var phone = localStorage.getItem('phone');
            if (username == null || username.length<2 || phone == null || phone.length != 11){
                alert("环境异常，请重试");
                return;
            }
            $.ajax({
                url:'/thmlfApi/loan/record',
                type:'POST',
                data:{'recordUsername':username,'recordPhone':phone,'typeId':id},
                success:function (result) {
                    if (result.code == 200)
                        if (vm.checkUrl(url)){
                            window.location.href = url;
                        }else
                            mui.alert("咨询方式:"+url);
                    else
                        alert(result.message);
                },
                error:function (e) {
                    console.error("error",e);
                    alert("请求失败");
                }
            })
        }
    }
});